<!DOCTYPE html>
<html>
<head>
    {include file="../application/common/header.html" /}
</head>

<body>
    <table id="exam_score_templatedg">
        <thead>
            <tr>
                <th data-options="field:'班号',width:50">班号</th>
                <th data-options="field:'学号',width:50">学号</th>
                <th data-options="field:'姓名',width:50">姓名</th>
                <th data-options="field:'学科',width:50">学科</th>
                <th data-options="field:'总分',width:50">总分</th>
            </tr>
        </thead>
    </table>
    <div id="exam_score_templatetb">
        <form id="exam_score_templateForm" class="m5">
            <div>
                <?php echo currTerm()["termName"]?>
                <select id="examId" class="easyui-combobox" data-options="
                panelHeight:'auto',
                value:'请选择', 
                width: 150,
                editable:false,
                textField:'examName',
                valueField:'ID',
                url:'/examScore/exam_score_template/getExamList'"></select>
                <select id="subject" class="easyui-combobox" data-options="panelHeight:'auto',
                editable:false,
                width:80,
                textField:'gradeName',
                valueField:'gradeName',
                url:'/examScore/exam_score_template/getSubject'"></select>
                <a class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-search'" onclick="queryOk('exam_score_template')">查询</a>
                <a class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-save'" onclick="downloadExcel('exam_score_template')">下载学生登分模板</a>
            </div>
        </form>
    </div>

    <div id="uploadBox" style="display: none;height: auto;">
        <div class="m30">
            <form method="post" enctype="multipart/form-data" id="exam_score_templateUploadForm" action="">
                <div id="scoreTips">
                    
                </div>
                <div class="mt20">
                    <input class="easyui-filebox" data-options="buttonText:'选择文件'" id="files" name="files" style="width:250px;">
                </div>
            </form>
        </div>
        <div class="tnc m20">
            <a href="javascript:void(0)" class="easyui-linkbutton" onclick="uploadCommit('exam_score_template')" style="width: 200px;">上传</a>
        </div>
    </div>

    <script type="text/javascript">
        var url = "/examScore/exam_score_template";
        var dfcf = "exam_score_template";
        var objDfdg = $("#"+dfcf+"dg");
        $(function(){
            objDfdg.datagrid({
                url: url + "/pageQuery.html",
                rownumbers:true,
                striped:true,
                border:false,
                pagination:true,
                pageSize:30,
                fitColumns:true,
                fit:true,
                singleSelect: true,
                toolbar: '#exam_score_templatetb',
                onClickRow: clickRow,
                onAfterEdit: afterEdit,
                onLoadSuccess: onLoadSuccess
            })
        })
        var examId,subject;
        function queryOk(){
            examId = $('#examId').combobox('getValue');
            subject = $('#subject').combobox('getValue');
            if(examId && subject){
                $.post(url + "/getScoreParam",{examId:examId,subject:subject},function(data){
                    var txArr=data.split(",");
                    var zdArr=[{field:'班号',title:'班号'},{field:'学号',title:'学号'},{field:'姓名',title:'姓名'},{field:'学科',title:'学科'},{field:'总分',title:'总分'}];   
                    var fieldArray=[];
                    var txshu=txArr.length;
                    for(var i=0;i<=txshu-1;i++){
                        title=txArr[i];
                        name="题型"+(parseInt(i)+1);
                        fieldArray.push(name);
                        zdArr.push({field:name,title:title,width:50,formatter:function(value,row,index){
                            if(!value) row.name = "";
                            return value;
                        },editor:{type:'numberbox',options:{precision:1,min:-5,max:parseInt(txArr[i].split(":")[1])}}})
                    }
                    $("#exam_score_templatedg").datagrid({columns:[zdArr]});
                    $('#exam_score_templatedg').datagrid('load',{examId:examId,subject:subject,fields:fieldArray.toString()});
                })
            }else{
                $.messager.alert('操作提示','请选择考试名称和学科。');
            }
        }
        var kaiqiIndex=-1;
        function onLoadSuccess(){dgEditIndex[dfcf]=-1}
        function clickRow(rowIndex,rowData){
            if(dgEditIndex[dfcf]>-1){       
                kaiqiIndex=rowIndex;
                objDfdg.datagrid('endEdit',dgEditIndex[dfcf]);
            }else{
                objDfdg.datagrid('beginEdit',rowIndex);
                dgEditIndex[dfcf]=rowIndex;
            }   
        }
        function kaiqiEdit(rowIndex){
            objDfdg.datagrid('beginEdit',rowIndex);
            dgEditIndex[dfcf]=rowIndex; 
        }

        function afterEdit(rowIndex,rowData,changes){  
            for(var i in changes){
                if(!changes[i]) delete changes[i];
            } 
            if(!jQuery.isEmptyObject(changes)){
                rowData.考试ID = examId;
                rowData.学科 = subject;
                $.ajax({type:"POST",url:url+"/addUpdate.html",data:rowData,success:function(r){
                    objDfdg.datagrid('updateRow',{index:rowIndex,row:{总分:r}});
                    kaiqiEdit(kaiqiIndex);
                }}) 
            }else{kaiqiEdit(kaiqiIndex)}
        }
        function downloadExcel(cf) {
            var examId = $('#examId').combobox('getValue');
            var subject = $('#subject').combobox('getValue');
            if(examId && subject){
                var examName = $('#examId').combobox('getText');
                $.post(url + "/getScoreParam",{examId:examId,subject:subject},function(data){
                    var txArr=data.split(",");
                    var fieldArray=[];
                    var txshu=txArr.length;
                    for(var i=0;i<=txshu-1;i++){
                        name="题型"+(parseInt(i)+1);
                        fieldArray.push(name);
                    }
                    window.location.href = url+"/exportExcel.html?examId=" + examId + "&subject=" + subject + "&examName=" + examName + "&fields=" + fieldArray.toString();
                });
            }else{
                $.messager.alert('操作提示','请选择考试名称和学科。');
            }
        }
        function importExcel(cf) {
            var examId = $('#examId').combobox('getValue');
            var subject = $('#subject').combobox('getValue');
            if(examId && subject){
                var examName = $('#examId').combobox('getText');
                $("#scoreTips").html("<span class='calendar-sunday'>确认选择上传" + "(" + examName + " " + subject+")" + "的成绩，以免成绩上传出错</span>");
                $('#uploadBox').dialog({
                    title : '上传成绩',
                    modal : true,
                    width : 400,
                    top : 100,
                    buttons : '#btns'
                }).show();
            }else{
                $.messager.alert('操作提示','请选择考试名称和学科。');
            }
        }
        function uploadCommit(cf){
            var examId = $('#examId').combobox('getValue');
            var subject = $('#subject').combobox('getValue');
            $.messager.progress({
                title : '提示',
                msg : '上传中，请稍候...'
            });
            $('#'+cf+'UploadForm').form('submit',{
                url: url + "/importExcel.html?examId="+examId+"&subject=" + subject,
                success:function(data){
                $.messager.progress('close');
                if(data == '000') {
                    $.messager.alert('提示', "上传成功");
                    $('#uploadBox').dialog("close");
                    queryOk();
                    return;
                }
                $.messager.alert('提示', data);
            }})
        }
    </script>
</body>
</html>